﻿
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace projetoFinal
{
    public class operaNivelAcessoModulo
    {
        public static List<nivelAcessoModulo> listar(string field, string value, int crit)
        {
            List<nivelAcessoModulo> listModulo = new List<nivelAcessoModulo>();
            string strSQL = string.Empty;

            if (field.Equals(""))
            {
                strSQL = " SELECT * FROM nivelacessomodulo";
            }
            else
            {
                strSQL = DAOHelper.sqlQueryBuilder("nivelacessomodulo", field, value, crit);
            }

            using (IDataReader dr = DAOHelper.executeQuery(strSQL))
            {
                while (dr.Read())
                {
                    nivelAcesso objNivelAcesso = operaNivelAcesso.buscar(Int64.Parse(dr["codNivelAcesso"].ToString()));
                    modulo objModulo = operaModulo.buscar(Int64.Parse(dr["codModulo"].ToString()));
                    nivelAcessoModulo objNivelAcessoModulo = new nivelAcessoModulo(Int64.Parse(dr["codNivelAcessoModulo"].ToString()), objNivelAcesso, objModulo, int.Parse(dr["pCreate"].ToString()) == 1 ? true : false, int.Parse(dr["pRead"].ToString()) == 1 ? true : false, int.Parse(dr["pUpdate"].ToString()) == 1 ? true : false, int.Parse(dr["pDelete"].ToString()) == 1 ? true : false);

                    listModulo.Add(objNivelAcessoModulo);
                }
            }
            return listModulo;
        }
        public static int incluir(nivelAcessoModulo objNivelAcessoModulo)
        {
            StringBuilder strSQL = new StringBuilder();
            strSQL.Append(" INSERT INTO ");
            strSQL.Append(" nivelacessomodulo ");
            strSQL.Append(" ( ");
            strSQL.Append("     codNivelAcesso, codModulo, pCreate, pRead, pUpdate, pDelete ");
            strSQL.Append(" ) ");
            strSQL.Append(" VALUES ");
            strSQL.Append(" ( ");
            strSQL.AppendFormat("   {0} ", objNivelAcessoModulo.NivelAcesso.CodNivelAcesso);
            strSQL.AppendFormat(" , {0} ", objNivelAcessoModulo.Modulo.CodModulo);
            strSQL.AppendFormat(" , {0} ", objNivelAcessoModulo.Create == true ? 1 : 0);
            strSQL.AppendFormat(" , {0} ", objNivelAcessoModulo.Read == true ? 1 : 0);
            strSQL.AppendFormat(" , {0} ", objNivelAcessoModulo.Update == true ? 1 : 0);
            strSQL.AppendFormat(" , {0} ", objNivelAcessoModulo.Delete == true ? 1 : 0);
            strSQL.Append("  ); ");
            return DAOHelper.executeDML(strSQL.ToString());
        }
        public static int apagar(Int64 codNivelAcesso)
        {
            StringBuilder strSQL = new StringBuilder();
            strSQL.Append(" DELETE FROM nivelacessomodulo ");
            strSQL.Append(" WHERE ");
            strSQL.Append(" codNivelAcesso = ");
            strSQL.AppendFormat("{0}; ", codNivelAcesso);
            return DAOHelper.executeDML(strSQL.ToString());
        }
        public static int editar(nivelAcessoModulo objNivelAcessoModulo)
        {
            StringBuilder strSQL = new StringBuilder();
            strSQL.Append(" UPDATE nivelacessomodulo ");
            strSQL.Append(" SET ");
            strSQL.AppendFormat("   codNivelAcesso = {0} ", objNivelAcessoModulo.NivelAcesso.CodNivelAcesso);
            strSQL.AppendFormat(" , codModulo = {0} ", objNivelAcessoModulo.Modulo.CodModulo);
            strSQL.AppendFormat(" , pCreate = {0} ", objNivelAcessoModulo.Create == true ? 1 : 0);
            strSQL.AppendFormat(" , pRead = {0} ", objNivelAcessoModulo.Read == true ? 1 : 0);
            strSQL.AppendFormat(" , pUpdate = {0} ", objNivelAcessoModulo.Update == true ? 1 : 0);
            strSQL.AppendFormat(" , pDelete = {0} ", objNivelAcessoModulo.Delete == true ? 1 : 0);
            strSQL.Append(" WHERE ");
            strSQL.AppendFormat(" codNivelAcessoModulo = {0}; ", objNivelAcessoModulo.CodNivelAcessoModulo);
            return DAOHelper.executeDML(strSQL.ToString());
        }
        public static nivelAcessoModulo buscar(Int64 codNivelAcessoModulo)
        {
            nivelAcessoModulo objNivelAcessoModulo;
            List<nivelAcessoModulo> listaNivelModulo = operaNivelAcessoModulo.listar(" codNivelAcessoModulo ", codNivelAcessoModulo.ToString(), 1);
            if (listaNivelModulo.Count == 1)
            {
                objNivelAcessoModulo = listaNivelModulo[0];
            }
            else
            {
                objNivelAcessoModulo = null;
            }
            return objNivelAcessoModulo;
        }
        public static nivelAcessoModulo buscar(modulo objModulo, string codUsuario)
        {
            nivelAcessoModulo objNivelAcessoModulo = null;

            if ((objModulo != null) && (codUsuario != string.Empty))
            {
                usuario objUsuario = operaUsuario.buscar(Int64.Parse(codUsuario.ToString()));
                string strSQL = DAOHelper.sqlQueryBuilder("nivelAcessoModulo", "codModulo = " + objModulo.CodModulo.ToString() + " AND codNivelAcesso ", objUsuario.NivelAcesso.CodNivelAcesso.ToString(), 1);
                using (IDataReader dr = DAOHelper.executeQuery(strSQL))
                {
                    if (dr.Read())
                    {
                        objNivelAcessoModulo = new nivelAcessoModulo(Int64.Parse(dr["codNivelAcessoModulo"].ToString()), objUsuario.NivelAcesso, objModulo, int.Parse(dr["pCreate"].ToString()) == 1 ? true : false, int.Parse(dr["pRead"].ToString()) == 1 ? true : false, int.Parse(dr["pUpdate"].ToString()) == 1 ? true : false, int.Parse(dr["pDelete"].ToString()) == 1 ? true : false);
                    }
                }
            }

            return objNivelAcessoModulo;
        }
        public static nivelAcessoModulo buscar(modulo objModulo, Int64 codNivelAcesso)
        {
            nivelAcessoModulo objNivelAcessoModulo = null;

            if ((objModulo != null) && (codNivelAcesso != 0))
            {
                string strSQL = DAOHelper.sqlQueryBuilder("nivelAcessoModulo", "codModulo = " + objModulo.CodModulo.ToString() + " AND codNivelAcesso ", codNivelAcesso.ToString(), 1);
                using (IDataReader dr = DAOHelper.executeQuery(strSQL))
                {
                    if (dr.Read())
                    {
                        nivelAcesso objNivelAcesso = operaNivelAcesso.buscar(codNivelAcesso);
                        objNivelAcessoModulo = new nivelAcessoModulo(Int64.Parse(dr["codNivelAcessoModulo"].ToString()), objNivelAcesso, objModulo, int.Parse(dr["pCreate"].ToString()) == 1 ? true : false, int.Parse(dr["pRead"].ToString()) == 1 ? true : false, int.Parse(dr["pUpdate"].ToString()) == 1 ? true : false, int.Parse(dr["pDelete"].ToString()) == 1 ? true : false);
                    }
                }
            }

            return objNivelAcessoModulo;
        }
    }
}
